package org.rsbot.loader.script.adapter;

import org.rsbot.loader.asm.ClassAdapter;
import org.rsbot.loader.asm.ClassVisitor;
import org.rsbot.loader.asm.MethodVisitor;
import org.rsbot.loader.asm.Opcodes;

public class AddGetterAdapter extends ClassAdapter
  implements Opcodes
{
  private boolean virtual;
  private Field[] fields;
  private String owner;

  public AddGetterAdapter(ClassVisitor paramClassVisitor, boolean paramBoolean, Field[] paramArrayOfField)
  {
    super(paramClassVisitor);
    this.virtual = paramBoolean;
    this.fields = paramArrayOfField;
  }

  public void visit(int paramInt1, int paramInt2, String paramString1, String paramString2, String paramString3, String[] paramArrayOfString)
  {
    this.owner = paramString1;
    this.cv.visit(paramInt1, paramInt2, paramString1, paramString2, paramString3, paramArrayOfString);
  }

  public void visitEnd()
  {
    Field localField;
    if (this.virtual) {
      for (localField : this.fields)
        visitGetter(localField.getter_access, localField.getter_name, localField.getter_desc, localField.name, localField.desc);
    }
    else {
      for (localField : this.fields) {
        visitGetter(localField.getter_access, localField.getter_name, localField.getter_desc, localField.owner, localField.name, localField.desc);
      }
    }
    this.cv.visitEnd();
  }

  private void visitGetter(int paramInt, String paramString1, String paramString2, String paramString3, String paramString4)
  {
    MethodVisitor localMethodVisitor = this.cv.visitMethod(paramInt, paramString1, paramString2, null, null);
    localMethodVisitor.visitCode();
    localMethodVisitor.visitVarInsn(25, 0);
    localMethodVisitor.visitFieldInsn(180, this.owner, paramString3, paramString4);
    int i = getReturnOpcode(paramString4);
    localMethodVisitor.visitInsn(i);
    localMethodVisitor.visitMaxs((i == 173) || (i == 175) ? 2 : 1, (paramInt & 0x8) == 0 ? 1 : 0);
    localMethodVisitor.visitEnd();
  }

  private void visitGetter(int paramInt, String paramString1, String paramString2, String paramString3, String paramString4, String paramString5)
  {
    MethodVisitor localMethodVisitor = this.cv.visitMethod(paramInt, paramString1, paramString2, null, null);
    localMethodVisitor.visitCode();
    localMethodVisitor.visitFieldInsn(178, paramString3, paramString4, paramString5);
    int i = getReturnOpcode(paramString5);
    localMethodVisitor.visitInsn(i);
    localMethodVisitor.visitMaxs((i == 173) || (i == 175) ? 2 : 1, (paramInt & 0x8) == 0 ? 1 : 0);
    localMethodVisitor.visitEnd();
  }

  private int getReturnOpcode(String paramString) {
    paramString = paramString.substring(paramString.indexOf(")") + 1);
    if (paramString.length() > 1) {
      return 176;
    }
    int i = paramString.charAt(0);
    switch (i) {
    case 66:
    case 67:
    case 73:
    case 83:
    case 90:
      return 172;
    case 74:
      return 173;
    case 70:
      return 174;
    case 68:
      return 175;
    case 69:
    case 71:
    case 72:
    case 75:
    case 76:
    case 77:
    case 78:
    case 79:
    case 80:
    case 81:
    case 82:
    case 84:
    case 85:
    case 86:
    case 87:
    case 88:
    case 89: } throw new RuntimeException("eek");
  }

  public static class Field
  {
    public int getter_access;
    public String getter_name;
    public String getter_desc;
    public String owner;
    public String name;
    public String desc;
  }
}